#!/bin/bash

currentdir=$(cd "$(dirname "$0")"; pwd)
echo "build directory: ${currentdir}."

echo -e "\nbuild pcre..."
if [ ! -d "$currentdir/build/linux" ]; then 
	mkdir -p "$currentdir/build/linux"
else
	rm -rf ${currentdir}/build/linux/*
fi

NDK="/opt/harmony-toolchain/llvm/bin"
# Tell configure what tools to use.
target_host=arm-linux-ohos
export AR=$NDK/llvm-ar
export AS=$NDK/llvm-as
export CC=$NDK/clang
export CXX=$NDK/clang++
export LD=$CC
export RANLIB=$NDK/llvm-ranlib
export STRIP=$NDK/llvm-strip


# Tell configure what flags Android requires.
export CFLAGS="-fPIC --target=$target_host -march=armv7-a"
export CPPFLAGS="-fPIC --target=$target_host -march=armv7-a"
export LDFLAGS="--target=$target_host"

autoreconf
./configure  --enable-utf8 --enable-shared=no --prefix=/usr/local --host $target_host
if [ $? -ne 0 ]; then exit 1; fi
make
make install DESTDIR=${currentdir}/build/linux

if [ ! -f "${currentdir}/build/linux/usr/local/lib/libpcre.a" ]; then exit 1; fi
rm -rf ${currentdir}/../../../lib/libpcr*.a
cp -rv "${currentdir}/build/linux/usr/local/lib/libpcre.a" ${currentdir}/../../../lib
cp -rv "${currentdir}/build/linux/usr/local/lib/libpcrecpp.a" ${currentdir}/../../../lib
cp -rv "${currentdir}/build/linux/usr/local/lib/libpcreposix.a" ${currentdir}/../../../lib

make clean
rm -rf ${currentdir}/build/linux/*

echo "====== pcre build success! ======"
exit 0
